package com.aletto.general_store.presenters;

import android.app.Activity;

import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.Response;
import com.aletto.general_store.data.model.Upload;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.presenters.views.ProgressView;
import com.aletto.general_store.utils.event.AddGoodsEvent;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.functions.Function;

public class AddGoodsTask extends SimpleTemplate<Object> {

    private final Activity context;

    private final int number, goodsType;

    private final String name, label, price;

    private final List<String> coverImages, introduceImages;

    public AddGoodsTask(Activity context, String name, String label, String price, int number, int goodsType, List<String> coverImages, List<String> introduceImages) {
        this.context = context;

        this.number = number;
        this.goodsType = goodsType;

        this.name = name;
        this.label = label;
        this.price = price;

        this.coverImages = coverImages;
        this.introduceImages = introduceImages;
    }

    @Override
    public INetTask.View<Object> view() {
        return new ProgressView<>(context) {
            @Override
            public void success(Object data) {
                context.finish();
                EventBus.getDefault().post(new AddGoodsEvent());
            }
        };
    }

    @Override
    public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
        // 上传封面
        Observable<String> coverTask = Observable.fromArray(coverImages.toArray(new String[0]))
                .map(File::new)
                .flatMap((Function<File, ObservableSource<Response<Upload>>>) file -> DataRepository.getInstance().upload(file))
                .toList()
                .map(responses -> responses.stream().map(item -> item.getData().getUrl()).collect(Collectors.toList()))
                .map(strings -> String.join(",", strings))
                .flatMapObservable((Function<String, ObservableSource<String>>) Observable::just);

        // 上传简介
        Observable<String> introduceTask = Observable.fromArray(introduceImages.toArray(new String[0]))
                .map(File::new)
                .flatMap((Function<File, ObservableSource<Response<Upload>>>) file -> DataRepository.getInstance().upload(file))
                .toList()
                .map(responses -> responses.stream().map(item -> item.getData().getUrl()).collect(Collectors.toList()))
                .map(strings -> String.join(",", strings))
                .flatMapObservable((Function<String, ObservableSource<String>>) Observable::just);

        return new NetSingleTask<>(
                Observable.merge(coverTask, introduceTask)
                        .toList()
                        .flatMapObservable((Function<List<String>, ObservableSource<List<String>>>) Observable::just)
                        .flatMap((Function<List<String>, ObservableSource<Response<Object>>>) strings ->
                                DataRepository.getInstance().addGoods(
                                        goodsType,
                                        name,
                                        strings.get(0),
                                        label,
                                        strings.get(1),
                                        Float.parseFloat(price),
                                        number, null))
        );

    }

}
